package nl.stroep.classbuilder.cb
{
	import flash.utils.ByteArray;
	import nl.stroep.classbuilder.cb.interfaces.ICbObject;
	import nl.stroep.classbuilder.cb.interfaces.ICbWrapper;
	import nl.stroep.classbuilder.definitions.Chars;
	import nl.stroep.classbuilder.definitions.DataTypes;
	import nl.stroep.classbuilder.templates.Templates;
	import nl.stroep.classbuilder.templates.TemplateVariableDefinitions;
	import nl.stroep.classbuilder.utils.ObjectUtil;
	import nl.stroep.classbuilder.utils.XMLUtil;
	
	/**
	 * Parameter definition
	 * @author Mark Knol - Stroep.nl - 2010 (c)
	 */
	public final class CbParameter implements ICbWrapper
	{
		private var _name:String;
		private var _type:String;
		private var _defaultValue:String;
		
		public var addToFunction:Boolean = false;
		public var addToClass:Boolean = false;
		
		public function CbParameter( name:String = null, type:String = null, defaultValue:String = null, addToFunction:Boolean = false, addToClass:Boolean = false ):void
		{ 
			this._name = name;
			this._type = type;
			this._defaultValue =  defaultValue;
			this.addToFunction = addToFunction;
			this.addToClass = addToClass;
		}
		
		public function toString( ):String 
		{
			var retval:String = Templates.getInstance().parameterTemplate;
			
			retval = retval.replace(TemplateVariableDefinitions.NAME, this._name);
			retval = retval.replace(TemplateVariableDefinitions.RETURNTYPE, this._type);
			retval = retval.replace(TemplateVariableDefinitions.DEFAULT_VALUE, (this._defaultValue ? (Chars.EQUAL + (this._type == DataTypes.STRING ? Chars.QUOTE + this._defaultValue + Chars.QUOTE : this._defaultValue)  ) : Chars.EMPTY));
			
			return retval;
		}
		
		public function toXML():XML
		{
			var xml:XML = <parameter/>;
			
			xml.appendChild( XMLUtil.getNode("name", this._name) );
			xml.appendChild( XMLUtil.getNode("type", this._type) );
			xml.appendChild( XMLUtil.getNode("defaultValue", this._defaultValue) );
			xml.appendChild( XMLUtil.getNode("addToFunction", addToFunction) );
			xml.appendChild( XMLUtil.getNode("addToClass", addToClass) );
			
			return xml;
		}
		
		public static function parseItem(xml:XML):CbParameter
		{
			var param:CbParameter = new CbParameter(
				ObjectUtil.parseString( xml.name.text() ),
				ObjectUtil.parseString( xml.type.text() ),
				ObjectUtil.parseString( xml.defaultValue.text() ),
				ObjectUtil.parseBoolean( xml.addToFunction.text() ),
				ObjectUtil.parseBoolean( xml.addToClass.text())
			)
			
			return param;
		}
		
		public static function parseData(xml:XMLList):Vector.<CbParameter> 
		{
			var retval:Vector.<CbParameter> = new Vector.<CbParameter>();
			
			if (xml.parameter.length() > 0)
			{
				const total:uint = xml.parameter.length();
				
				for (var i:int = 0; i < total; ++i) 
				{
					var param:CbParameter = CbParameter.parseItem(xml.parameter[i]);
					retval.push(param);
				}
			}
			
			return retval;
		}
		
		public function get name():String { return _name; }
		
		public function set name(value:String):void 
		{
			_name = value;
		}
		
		public function get type():String { return _type; }
		
		public function set type(value:String):void 
		{
			_type = value;
		}
		
		public function get defaultValue():String { return _defaultValue; }
		
		public function set defaultValue(value:String):void 
		{
			_defaultValue = value;
		}
		
	}
}